
from gremlin_python import statics
from gremlin_python.process.anonymous_traversal import traversal
from gremlin_python.process.graph_traversal import __
from gremlin_python.process.strategies import *
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
from gremlin_python.structure.graph import Graph
from gremlin_python.process.traversal import T
from gremlin_python.process.traversal import Order
from gremlin_python.process.traversal import Cardinality
from gremlin_python.process.traversal import Column
from gremlin_python.process.traversal import Direction
from gremlin_python.process.traversal import Operator
from gremlin_python.process.traversal import P
from gremlin_python.process.traversal import TextP
from gremlin_python.process.traversal import Pop
from gremlin_python.process.traversal import Scope
from gremlin_python.process.traversal import Barrier
from gremlin_python.process.traversal import Bindings
from gremlin_python.process.traversal import WithOptions
#模拟direct，barrier，endtrack来设置追踪路径

def direct_set(direct):
    if direct not in ["forward","backward"]:
        print("direct设置有问题,已设置为默认选项forward")
        return "forward"
    return direct


def barrier_set(barrier):
    if barrier not in ["cfg","dfg"]:
        print("barrier设置有问题,已设置为默认选项cfg")
        return "cfg"
    return barrier


def end_set(v_end):
    if len(v_end)==0:
        return None
    else:
        return v_end

def forward_track_no_end_set(g,v_start,barrier):
    simplePaths={}
    for v in v_start:
        simplePath = g.V(v_start).repeat(__.out(barrier).simplePath()).until(__.not_(__.out(barrier))).path().toList()
        simplePaths[v]=simplePath
    return simplePaths

def backward_track_no_end_set(g,v_start,barrier):
    simplePaths={}
    for v in v_start:
        simplePath = g.V(v_start).repeat(__.in_(barrier).simplePath()).until(__.not_(__.in_(barrier))).path().toList()
        simplePaths[v]=simplePath
    return simplePaths

def track_run(g,v_start,direct,barrier,v_end=None):
    direct = direct_set(direct)
    barrier = barrier_set(barrier)
    if(v_end==None):
        if direct=="forward":
            return forward_track_no_end_set(g,v_start,barrier)
        elif direct=="backward":
            return backward_track_no_end_set(g,v_start,barrier)
        else:
            print("to do ,now can't use")
            return 
    else:
        print("to do ,now can't use")
        return

        
